package com.qiku.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiku.dao.UserMapper;
import com.qiku.domin.*;
import com.qiku.service.UserService;
import com.qiku.utils.MD5;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public PageInfo<User> findAllUserByPage(UserVO userVO) {

        PageHelper.startPage(userVO.getCurrentPage() , userVO.getPageSize());
        List<User> allUserByPage = userMapper.findAllUserByPage(userVO);
        PageInfo<User> userPageInfo = new PageInfo<>(allUserByPage);

        return userPageInfo;
    }

    @Override
    public void updateUserStatus(Integer id , String status) {
        User user = new User();
        user.setUpdate_time(new Date());
        user.setId(id);
        user.setStatus(status);
        userMapper.updateUserStatus(user);
    }

    @Override
    public User login(UserVO userVO) throws Exception {
        User user = new User();
        // 对象属性的拷贝   拷贝后面的 赋值给前面的
        BeanUtils.copyProperties(user , userVO);
        User login = userMapper.login(user);

        // 验证 密码是否正确
        if (login != null && MD5.verify(user.getPassword() , "qiku" , login.getPassword())){
            return login;
        }else {
            return null;
        }

    }


    @Override
    public List<Role> findUserRelationRoleById(Integer id) {
        List<Role> roleList = userMapper.findUserRoleRelationById(id);
        return roleList;
    }



    @Override
    public void userContentRole(UserVO userVO) {
        //  清空当前用户关联的角色
        userMapper.deleteUserContentRole(userVO.getUserId());
        // 分配角色 （向表中插入数据）
        // 获取 需要分配的角色id的集合
        List<Integer> roleIdList = userVO.getRoleIdList();
        for (Integer roleId : roleIdList){
            User_Role_relation user_role_relation = new User_Role_relation();
            user_role_relation.setUserId(userVO.getUserId());
            user_role_relation.setRoleId(roleId);
            // 补全信息
            Date date = new Date();
            user_role_relation.setCreatedTime(date);
            user_role_relation.setUpdatedTime(date);
            user_role_relation.setCreatedBy("admin");
            user_role_relation.setUpdatedby("admin");
            // 向中间表插入数据
            userMapper.userContentRole(user_role_relation);
        }


    }

    @Override
    public ResponseResult getUserPermissions(Integer id) {
        // 获取当前用户的角色列表
        List<Role> roleList = userMapper.findUserRoleRelationById(id);
        // 将获取到的角色的id 存入list
        List<Integer> roleIdList = new ArrayList<>();
        for (Role role : roleList){
            roleIdList.add(role.getId());
        }
        // 3根据角色id 查询 父菜单
        List<Menu> parentmenuList = userMapper.findParentMenuByRoleId(roleIdList);
        // 4封装 父菜单的下级菜单
        for (Menu menu : parentmenuList){
            // 获取当前菜单的子菜单
            List<Menu> subMenuList = userMapper.findSubMenuByPid(menu.getId());
            menu.setSubMenuList(subMenuList);
        }

        // 5获取资源权限
        List<Resource> resourceList = userMapper.findResourceByRoleId(roleIdList);
        // 6封装数据
        Map<String , Object> map = new HashMap<>();
        map.put("menuList" , parentmenuList);
        map.put("resourceList" , resourceList);;
        ResponseResult result = new ResponseResult(true, 200 ,"响应成功", map);
        return result;
    }


}
